#!/usr/bin/env python

try:
    # dumb workaround for py2exe
    import pygame._view
except:
    pass

import sys, traceback
import parole
import main

crashMessage = \
"""
Oopsies! Nyctos has crashed. Please report this as a bug at
http://nyctos.googlecode.com, and include the file full.log in your report.
""".strip()

def start(defaultCfg='nyctos.cfg'):
    # clear the log file
    f = open('full.log', 'w')
    f.close()

    # open the log file in append mode
    f = open('full.log', 'a')

    # send any standard error output to the log file.
    sys.stderr = f

    # requested config file?
    cfg = len(sys.argv) > 1 and sys.argv[1] or defaultCfg
    main.configFileName = cfg
    try:
        # start things up
        parole.startup(cfg, main.doFrame, caption="Nyctos",
                icon='icon.png', gen=True)
    except:
        # send exception stacktraces to the log file (in case this isn't already
        # handled by sending stderr there; stupid windows)
        traceback.print_exc(file=f)
        print crashMessage
        sys.stderr.write(crashMessage + '\n')
        print 'Attempting to display the crash GUI...'
        sys.stdout.flush()
        sys.stderr.flush()

        # Attempt to display the crash message in a GUI using Tkinter
        import Tkinter as tk
        root = tk.Tk()
        w = tk.Label(root, text=crashMessage)
        w.pack()
        root.mainloop()
        

if __name__ == "__main__":
    start()
